#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       id3.py
#       
#       Copyright 2012 Diego Rivas <diego@diego>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#       
#       

import string

class id3:
	
	def __init__(self, path):
		self.path = path
		self.__parse_tag()
	
	def __strip_padding(self, s):
		while len(s) > 0 and s[-1] in string.whitespace + "\0":
			s = s[:-1]

		return s
		
	def __getGenre(self, n):
		genres = [ 
				"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", 
				"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", 
				"Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", 
				"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", 
				"Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", 
				"Trance", "Classical", "Instrumental", "Acid", "House", "Game", 
				"Sound Clip", "Gospel", "Noise", "Alt. Rock", "Bass", "Soul", 
				"Punk", "Space", "Meditative", "Instrum. Pop", "Instrum. Rock", 
				"Ethnic", "Gothic", "Darkwave", "Techno-Indust.", "Electronic", 
				"Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", 
				"Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", 
				"Native American", "Cabaret", "New Wave", "Psychadelic", "Rave", 
				"Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", 
				"Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", 
				"Folk/Rock", "National Folk", "Swing", "Fusion", "Bebob", "Latin", 
				"Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", 
				"Progress. Rock", "Psychadel. Rock", "Symphonic Rock", "Slow Rock", 
				"Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", 
				"Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", 
				"Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", 
				"Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", 
				"Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", 
				"A Capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass", 
				"Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk", 
				"Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal", 
				"Black Metal", "Crossover", "Contemporary Christian", "Christian Rock",
				"Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop", "Synthpop" 
				]
				
		return genres[n]
	
	def __parse_tag(self):
		try:
		  mp3 = open(self.path,'r+b')
		  try:
			mp3.seek(-128,2)
			dump = mp3.read(3)
			self.name = self.__strip_padding(mp3.read(30))
			self.artist = self.__strip_padding(mp3.read(30))
			self.album = self.__strip_padding(mp3.read(30))
			self.year = self.__strip_padding(mp3.read(4))
			self.comment = self.__strip_padding(mp3.read(30))
			self.genre = self.__getGenre(ord(self.__strip_padding(mp3.read(1))))
		  finally:
			mp3.close()
		except IOError:
		  print ("IO Error");
		  exit()
		
